/**
 * 
 */
package com.shine.icis.dao.imp;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.sql.hibernate.CommQuery;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.dao.IOfConfirmProcDAO;
import com.shine.icis.dao.imp.BaseHibernateDAO;
import com.shine.icis.dto.ReturnMsg;
import com.shine.icis.hbean.OfConfirmProc;
import com.shine.icis.hbean.OfConfirmProcDetail;

/**
 * 
 * @author zsy
 *
 */
public class OfConfirmProcDAO extends BaseHibernateDAO implements IOfConfirmProcDAO {
	
	@Override
	public ListPage<OfConfirmProc> findListPage(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException("非法参数:查询参数对象为null");
		}
		try {
			StringBuffer sbQueryString = new StringBuffer();			
			sbQueryString.append("select l from OfConfirmProc as l where l.arapType not in (:arapTypes)");
			qp.addParameter("arapTypes", new String[]{OfConfirmProc.ARAP_TYPE_A0, OfConfirmProc.ARAP_TYPE_A1});
			if(qp.getParameter("oppPar") != null){
				sbQueryString.append(" and l.oppPar=:oppPar");
			}
			if(qp.getParameter("arapBusiType") != null){
				sbQueryString.append(" and l.arapBusiType=:arapBusiType");
			}
			if(qp.getParameter("busiDate") != null){
				sbQueryString.append(" and l.busiDate=:busiDate");
			}
			
			return new CommQuery<OfConfirmProc>().queryListPage(qp, 
					qp.appendOrders(sbQueryString, "l" ), getSession());
		} catch(RuntimeException re) {
			re.printStackTrace();
			return new  ListPage<OfConfirmProc>();
		}
	}

	@Override
	public OfConfirmProc findById(int id) {
		OfConfirmProc sl = (OfConfirmProc)getSession().get(OfConfirmProc.class, id);
		return sl;
	}
	
	@Override
	public void deleteById(int id) {
		Query query = getSession().createQuery("delete OfConfirmProcDetail d where d.procId=:procId");
		query.setInteger("procId", id);
		query.executeUpdate();
		query = getSession().createQuery("delete OfConfirmProc o where o.id=:id");
		query.setInteger("id", id);
		query.executeUpdate();
	}
	
	@Override
	public double findAmount(int busiDate, String arapType) {
		Query query = getSession().createQuery("select sum(o.arapAmt) from OfConfirmProc o " +
				"where o.busiDate=:busiDate and o.arapType=:arapType");
		query.setInteger("busiDate", busiDate);
		query.setString("arapType", arapType);
		Double d = (Double)query.uniqueResult();
		if (d == null) {
			return 0;
		}
		return d.doubleValue();
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public void callProc(String proSql, int date, String increment) {
		try {
			CallableStatement proc = getSession().connection().prepareCall(proSql);
			proc.setInt(1, 0);
			proc.setInt(2, date);
			proc.setInt(3, date);
			proc.setString(4, "1");
			proc.setInt(5, 0);
			proc.registerOutParameter(6, Types.VARCHAR);
			proc.registerOutParameter(7, Types.INTEGER);
			
			proc.execute();
		    String msg = proc.getString(6);
		    int code = proc.getInt(7);
		    proc.close();
		    if (code != ReturnMsg.CODE_SUCCESS) {
		    	throw new SQLException(msg);
		    }
		} catch (SQLException e) {
			e.printStackTrace();
			throw new HibernateException(e);
		}
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<OfConfirmProcDetail> findDetail(int id) {
		Query query = getSession().createQuery("select o from OfConfirmProcDetail o " +
				"where o.procId=:procId");
		query.setInteger("procId", id);
		return query.list();
	}
}
